package com.yuan.java.java.nio;

import com.yuan.java.java.io.IOTestDemo;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author yuaneg 2019/7/10
 */
public class Demo {

    @Test
    public void file() throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        FileChannel fileChannel = new FileInputStream(IOTestDemo.class.getResource("/demo/pomdemo.xml").getPath()).getChannel();
        ByteBuffer buf = ByteBuffer.allocate(1024);
        int bytesRead = fileChannel.read(buf);
        while (bytesRead != -1) {
            buf.flip();
            while (buf.hasRemaining()) {
                stringBuilder.append((char) buf.get());
            }
            buf.compact();
            bytesRead = fileChannel.read(buf);
        }
        System.out.println(stringBuilder.toString());
    }


}
